(
    function (factory) {
        if (typeof define === "function" && define.amd) {

            // AMD. Register as an anonymous module.
            define(["jquery", "./version"], factory);
        } else {

            // Browser globals
            factory(jQuery);
        }
    }
        (
        function ($) {
            var AppNav = function (op) {
                var _options = op;
                var state = {
                    'overlayOpen': false
                }

                function init() {
                    try {
                        //load template
                        if (_options.emptyOnInit)
                            $('.app-nav').empty();
                        var tpl = _options.template;
                        $(tpl).appendTo($('.app-nav'));

                        $('.app-nav').find('.apps-btn').click(function () {
                            state.overlayOpen = true;
                            refresh();
                        })

                        $('.app-nav').find('.back-btn').click(function () {
                            state.overlayOpen = false;
                            refresh();
                        })
                        //set app list
                        var apps = _options.apps;
                        var applist = $('.app-nav .app-list')
                        var appTpl = $('.app-nav .app-list .app-template')
                        appTpl.remove();
                        // $(applist).e(appTpl)
                        if (apps) {
                            apps.forEach(function (app) {
                                var html = appTpl.clone();
                                html.find('.app-link').attr('href', app.url)
                                html.find('.app-name').html(app.name)
                                if (app.icon) {
                                    html.find('.app-icon-image').addClass(app.icon)
                                }
                                else {
                                    html.find('.app-icon-image').addClass('ms-icon-cubes');
                                }
                                html.appendTo(applist);
                            });
                        }

                        //fix top bar
                        $('.nav-bar').sticky({
                            top: 0
                        })
                    } catch(e){

                    }

                    refresh();
                }

                function refresh() {
                    if (state.overlayOpen) {
                        $('.app-nav .overlay').removeClass('hide');
                        $('.app-nav .overlay-hide').addClass('hide');
                        $('.app-nav .overlay-show').removeClass('hide');
                        $('.app-wrapper').addClass('hide');
                    }
                    else {
                        $('.app-nav .overlay').addClass('hide');
                        $('.app-nav .overlay-hide').removeClass('hide');
                        $('.app-nav .overlay-show').addClass('hide');
                        $('.app-wrapper').removeClass('hide');
                    }
                }

                this.init = init;
            }

            AppNav.defaultOptions = {
                'emptyOnInit': true
            }

            var appNavInit = function () {
                $.get('config.json', function (data) {
                    var apps = data.apps;
                    $.get('templates/app-nav.html', function (tpl) {
                        var op = AppNav.defaultOptions;
                        op['apps'] = apps;
                        op['template'] = tpl;
                        var appNav = new AppNav(op);
                        instance = appNav;
                        instance.init();
                    })
                })
            }

            $(appNavInit)
        }
        )
)